package com.ruoyi.project.cwsk.datestat.service.impl;

import com.ruoyi.project.cwsk.datestat.domain.OverviewDto;
import com.ruoyi.project.cwsk.datestat.mapper.OverviewMapper;
import com.ruoyi.project.cwsk.datestat.service.OverviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class OverviewServiceImpl implements OverviewService {
    @Resource
    private OverviewMapper overviewMapper;

    @Override
    @Cacheable(cacheNames = "overview",key = "'time:'+#overviewDto.startDate+':'+#overviewDto.endDate")
    public List<OverviewDto> findOverview(OverviewDto overviewDto) {
        List<OverviewDto> list=overviewMapper.findOverview(overviewDto);
        Integer total_regUserCount=0;
        Integer total_activeUserCount=0;
        Integer total_payCount=0;
        Double total_incMoney=0d;
        Double total_newIncUserIncMoney=0d;
        Integer total_newIncUserIncCount=0;

        //计算 付费率、arpu
        for(OverviewDto dto:list){
            //活跃用户数
            dto.setActiveUserCount(dto.getRegUserCount() + dto.getActiveUserCount());
            //新增付费率  新增充值/新增人数
            if(dto.getRegUserCount()!=0){
                dto.setNewPayRate( (dto.getNewIncUserIncCount()+0.00000000001) / dto.getRegUserCount() );
            }
            //活跃付费率  充值人数/活跃人数
            if(dto.getActiveUserCount()!=0){
                dto.setActivePayRate( (dto.getPayCount()+0.00000000001) / dto.getActiveUserCount() );
            }
            //arppu  充值金额/充值人数
            if(dto.getPayCount()!=0){
                dto.setArppu( (dto.getIncMoney()-0.00000000001) / dto.getPayCount() );
            }
            //arpu  充值金额/活跃人数
            if(dto.getActiveUserCount()!=0){
                dto.setArpu( (dto.getIncMoney()-0.00000000001) / dto.getActiveUserCount() );
            }

            //总计
            total_regUserCount += dto.getRegUserCount();
            total_activeUserCount += dto.getActiveUserCount();
            total_payCount += dto.getPayCount();
            total_incMoney += dto.getIncMoney();
            total_newIncUserIncMoney += dto.getNewIncUserIncMoney();
            total_newIncUserIncCount += dto.getNewIncUserIncCount();

        }

        OverviewDto totalDto = new OverviewDto();
        totalDto.setStatDate("总计");
        totalDto.setRegUserCount(total_regUserCount);
        totalDto.setActiveUserCount(total_activeUserCount);
        totalDto.setPayCount(total_payCount);
        totalDto.setIncMoney(total_incMoney);
        totalDto.setNewIncUserIncMoney(total_newIncUserIncMoney);

        totalDto.setNewPayRate((total_newIncUserIncCount+0.00000000001) / total_regUserCount);
        totalDto.setActivePayRate((total_payCount+0.00000000001) / total_activeUserCount);
        totalDto.setArppu((total_incMoney-0.00000000001) / total_payCount);
        totalDto.setArpu((total_incMoney-0.00000000001) / total_activeUserCount);

        list.add(totalDto);


        return list;
    }

    @Override
    public List<OverviewDto> exportOverview(OverviewDto dto) {
        return overviewMapper.exportOverview(dto);
    }
}
